package com.example.design.bilibili.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * 类是核心
 * 1、包含最新的天气情况信息
 * 2、含有观察者集合，使用ArrayList进行管理
 * 3、当时数据有更新时，就主动调用，ArrayList通知所有的(接入方）就看到最新的信息
 */
public class WeatherData implements Subject{

    private float temperature;

    private float pressure;

    private float humidity;

    //观察者集合
    private List<Observer> observers;

    public WeatherData() {

        observers = new ArrayList<>();
    }

    public void dataChange(){

        //调用 接入方的update
        notifyObservers();
    }

    public void setData(float temperature,float pressure,float humidity){
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;

        //调用dataChange，将最新的信息推送给接入方currentConditions
        dataChange();
    }

    //注册一个观察者
    @Override
    public void registerObserver(Observer o) {
        observers.add(o);

    }

    //移除一个观察者
    @Override
    public void removeObserver(Observer o) {

        if(observers.contains(o)){
            observers.remove(o);
        }
    }

    //遍历所有观察者，并通知
    @Override
    public void notifyObservers() {

        for(int i = 0 ; i<observers.size();i++){
            observers.get(i).update(this.temperature,this.pressure,this.humidity);
        }
    }

    public float getTemperature() {
        return temperature;
    }

    public void setTemperature(float temperature) {
        this.temperature = temperature;
    }

    public float getPressure() {
        return pressure;
    }

    public void setPressure(float pressure) {
        this.pressure = pressure;
    }

    public float getHumidity() {
        return humidity;
    }

    public void setHumidity(float humidity) {
        this.humidity = humidity;
    }
}
